# -*- coding: windows-1252 -*-
import xmlrpclib
import xbmcgui
import thread
import time
import os
from sets import Set

class FriendWindow(xbmcgui.Window) :

	ACTION_PREVIOUS_MENU = 10

	def __init__(self) :
		self.initializeVariable()
		self.initializeComponent()
		
		self.setScreen()
	
	def initializeVariable(self) :
		self.proxy = xmlrpclib.ServerProxy('http://localhost:8001')
		self.currentDir = os.getcwd()
		self.imageDir = os.path.join(self.currentDir, 'image')
		
		self.conferenceSet = []
		self.activeCall = {}
		
		self.screenW = 1280
		self.screenH = 720
		
		self.scaleX = (float(self.getWidth()) / float(self.screenW))
		self.scaleY = (float(self.getHeight()) / float(self.screenH))
		
		pass
	def setScreen(self) :
		self.setCoordinateResolution(1)
		
		
	def initializeComponent(self):
		
		''' background and logo '''
		self.logoBox = xbmcgui.ControlImage( self.screenW - 300 , 50, 200,200, os.path.join( os.getcwd(), 'aia_logo.gif'))
		self.backBox = xbmcgui.ControlImage(0,0, self.screenW, self.screenH,  os.path.join(os.getcwd(), 'bg.png'))
		
		self.bgContact = xbmcgui.ControlImage( 0 , 0, 500,500, os.path.join( os.getcwd(), 'bgcontact.jpg'))

		self.addControl(self.backBox)
		self.addControl(self.bgContact)
		self.addControl(self.logoBox)
	
		''' friend list '''
		self.friendList = xbmcgui.ControlList(100,100,600,500,  imageWidth=50, imageHeight=50, 
			itemTextXOffset=0, textColor='0xFF000000', buttonFocusTexture =  os.path.join( os.getcwd(), 'backbutton.gif'))
		self.friendList.setAnimations([('focus', 'effect=zoom end=90,247,220,56 time=0',)])
		self.friendList.setPageControlVisible(True)
		
		self.addControl(self.friendList)
		self.setFocus(self.friendList)

		friends = self.proxy.getAllFriend()
		for friend in friends :
			skypeName = friend['SKYPE_NAME']
			status = friend['STATUS']
			
			image1 = os.path.join(self.imageDir, 'Call.png')
			image2 = os.path.join(self.imageDir, 'SkypeIcon.tbn')
		
			listitem = xbmcgui.ListItem(skypeName, status, image1 ,
				image1)#, path='f:\\movies\\casino_royale.mov')
			self.friendList.addItem(listitem)
			
		image = os.path.join(self.imageDir, 'camera-web.png')
		#self.imageBox = xbmcgui.ControlImage(800,30,200,200 , image)
		#self.addControl(self.imageBox)
		
		

		
	def onControl(self, control):
		if control == self.friendList :
			selectedItem = self.friendList.getSelectedItem()
			skypeName = selectedItem.getLabel()		
			status = selectedItem.getLabel2()
			if status != 'OFFLINE'  :
				if (status != 'Calling...') and (len(self.activeCall) == 0) :
					self.activeCall['SKYPE_NAME'] = skypeName
					self.activeCall['STATUS' ] = status
					
					selectedItem.setLabel2('Calling...')
					self.call(skypeName)
				elif status == 'Calling...' :
					selectedItem.setLabel2(self.activeCall['STATUS'])
					self.proxy.hangUp()
					#time.sleep(1)
					
					self.activeCall.clear()
				'''
				else :
					if len(self.activeCall) == 0 :
						self.activeCall['SKYPE_NAME'] = skypeName
						self.activeCall['STATUS'] = status
				'''	
					
		pass

	'''
	def onAction(self, action):
		if action == ACTION_PREVIOUS_MENU :
			self.close()
		else :
			self.call(selt.freindList.selectedItem.getLabel()) 
	'''
			
	def call(self,skypeName):
		#self.proxy = xmlrpclib.ServerProxy('http://localhost:8001/')

		self.proxy.call(skypeName)
	
f = FriendWindow()
f.doModal()
del(f)
